//
//  HomeVC.m
//  FuZhu
//
//  Created by app on 2017/10/12.
//  Copyright © 2017年 王文. All rights reserved.
//

#import "HomeVC.h"

@interface HomeVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIImageView *bgIV;
@property (nonatomic,strong)UIView *renovationView;//装修
@property (nonatomic,strong)UIView *loanView;//贷款
@property (nonatomic,strong)UIView *houseView;//买房
@end

@implementation HomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    [self initTableView];
    [self addHeaderView];
}
-(void)addHeaderView {
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 460*kHeightScale)];
    headView.backgroundColor = [UIColor colorWithWhite:245/255.0 alpha:1];
    
    UIImageView *headIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 300*kHeightScale)];
    headIV.userInteractionEnabled = YES;
    UIImage *headImage = [UIImage imageNamed:@"图层-195"];
    headIV.image = headImage;
    [headView addSubview:headIV];
    
    [self addHeaderSubView:headView];
    self.tableView.tableHeaderView = headView;
}
-(void)addHeaderSubView:(UIView *)headView {
    UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake(10, 200*kHeightScale, kWidth-20, 250*kHeightScale)];
    itemView.backgroundColor = [UIColor whiteColor];
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:itemView.bounds];
    itemView.layer.masksToBounds = NO;
    itemView.layer.shadowColor = [UIColor blackColor].CGColor;
    itemView.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
    itemView.layer.shadowOpacity = 0.2f;
    itemView.layer.shadowPath = shadowPath.CGPath;
    [headView addSubview:itemView];
    
    UIImageView *bgIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, itemView.width, 35*kHeightScale)];
    self.bgIV = bgIV;
    bgIV.userInteractionEnabled = YES;
    NSArray *titleArr = @[@"我要装修",@"我要贷款",@"我要买房"];
    for (int i = 0; i < 3; i++) {
        UIButton *titleBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, itemView.width, bgIV.height)];
        [titleBtn setTitle:titleArr[i] forState:UIControlStateNormal];
        titleBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [titleBtn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [bgIV addSubview:titleBtn];
    }
    
    //我要装修
    self.renovationView = [[UIView alloc]initWithFrame:CGRectMake(0, bgIV.bottom, itemView.width, itemView.height-bgIV.height)];
    self.renovationView.backgroundColor = [UIColor whiteColor];
    NSArray *itemTitleArr = @[@"找公司",@"找设计",@"找监理",@"家居/建材",@"免费设计",@"免费报价",@"装修保障",@"家电/饰品"];
    NSArray *itemImageArr = @[@"icon-_Find_-company",@"icon_Find_design",@"icon_Find_Supervisor",@"icon_furnishing",@"icon_free_design",@"icon_free_quote",@"icon_guarantee",@"icon_electric_appliance"];
    for (int i = 0; i < 8; i++) {
        UIButton *itemBtn = [[UIButton alloc]initWithFrame:CGRectMake(itemView.width/4*(i%4), self.renovationView.height/2*(i/4), itemView.width/4, self.renovationView.height/2)];
        itemBtn.backgroundColor = [UIColor whiteColor];
        [itemBtn addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImage *itemImage = [UIImage imageNamed:itemImageArr[i]];
        UIImageView *itemIV = [[UIImageView alloc]initWithFrame:CGRectMake(itemBtn.width/2-itemImage.size.width/2, 30*kHeightScale, itemImage.size.width, itemImage.size.height)];
        itemIV.image = itemImage;
        [itemBtn addSubview:itemIV];
        
        UILabel *itemLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, itemBtn.height-20*kHeightScale-15, itemBtn.width, 15)];
        itemLabel.textAlignment = NSTextAlignmentCenter;
        itemLabel.font = [UIFont systemFontOfSize:14];
        itemLabel.text = itemTitleArr[i];
        [itemBtn addSubview:itemLabel];
        
        [self.renovationView addSubview:itemBtn];
    }
    
    //我要贷款
    self.loanView = [[UIView alloc]initWithFrame:self.renovationView.frame];
    self.loanView.backgroundColor = [UIColor whiteColor];
    self.loanView.hidden = YES;
    
    //我要买房
    self.houseView = [[UIView alloc]initWithFrame:self.renovationView.frame];
    self.houseView.backgroundColor = [UIColor whiteColor];
    self.houseView.hidden = YES;
    
    [itemView addSubview:self.houseView];
    [itemView addSubview:self.loanView];
    [itemView addSubview:self.renovationView];
    
}
-(void)titleBtnClick:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"我要装修"]) {
        self.bgIV.image = [UIImage imageNamed:@""];
        self.renovationView.hidden = NO;
        self.loanView.hidden = YES;
        self.houseView.hidden = YES;
    }else if ([sender.titleLabel.text isEqualToString:@"我要贷款"]) {
        self.bgIV.image = [UIImage imageNamed:@""];
        self.renovationView.hidden = YES;
        self.loanView.hidden = NO;
        self.houseView.hidden = YES;
    }else {
        //我要买房
        self.bgIV.image = [UIImage imageNamed:@""];
        self.renovationView.hidden = YES;
        self.loanView.hidden = YES;
        self.houseView.hidden = NO;
    }
}
-(void)itemBtnClick:(UIButton *)sender {
    
}
-(void)initTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight-49) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.cellLayoutMarginsFollowReadableWidth = false;
    self.tableView.backgroundColor = [UIColor colorWithWhite:245/255.0 alpha:1];
    [self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 3) {
        return .0001f;
    }else {
        return 5;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return .0001f;
    }else {
        return 5;
    }
}
-(void)cellHeight:(CGFloat)height {
    self.tableView.rowHeight = height;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
